package com.google.android.material.appbar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.view.ViewCompat;

/** 解决fling问题（当NestedScrollView滑行后，AppBarLayout会卡在中间）*/
public class AppBarLayoutBehavior3 extends AppBarLayoutBehavior2
{
    public static boolean DEBUG = false;
    /**
     * ScrollingView在fling会触发onNestedScroll，却不一定触发onStopNestedScroll，故以此解决
     */
    Runnable mStopNestedFling;

    public AppBarLayoutBehavior3(){
    }

    public AppBarLayoutBehavior3(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
        View targetChild, View target, int axes, int type){
        if(DEBUG) Log.i("----", "onStartNestedScroll " + Arrays.asList(//
            "type",//
            type,//
            ""//
        ));
        if(mStopNestedFling != null) child.removeCallbacks(mStopNestedFling);
        return super.onStartNestedScroll(parent, child, targetChild, target, axes, type);
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout parent, AppBarLayout child, View target,
        int type){
        if(DEBUG) Log.i("----", "onStopNestedScroll " + Arrays.asList(//
            "type",//
            type,//
            ""//
        ));
        //RecyclerView在fling后会触发此方法，而NestedScrollView不会
        if(mStopNestedFling != null) child.removeCallbacks(mStopNestedFling);
        super.onStopNestedScroll(parent, child, target, type);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout parent, AppBarLayout child, View target,
        int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type,
        int[] consumed){
        super.onNestedScroll(parent, child, target, dxConsumed, dyConsumed, dxUnconsumed,
            dyUnconsumed, type, consumed);
        if(DEBUG) Log.i("----", "onNestedScroll " + Arrays.asList(//
            "dxConsumed",//
            dxConsumed,//
            "dyConsumed",//
            dyConsumed,//
            "dxUnconsumed",//
            dxUnconsumed,//
            "dyUnconsumed",//
            dyUnconsumed,//
            "consumed",//
            Arrays.toString(consumed),//
            "type",//
            type,//
            ""//
        ));
        if(type == ViewCompat.TYPE_NON_TOUCH){
            if(mStopNestedFling == null) mStopNestedFling = () -> {
                mStopNestedFling = null;
                super.onStopNestedScroll(parent, child, target, type);
            };
            child.removeCallbacks(mStopNestedFling);
            child.postDelayed(mStopNestedFling, 30);
        }
    }

    @Override
    public boolean onNestedFling(CoordinatorLayout parent, AppBarLayout child, View target,
        float velocityX, float velocityY, boolean consumed){
        if(DEBUG) Log.i("----", "onNestedFling " + Arrays.asList(//
            "velocityX",//
            velocityX,//
            "velocityY",//
            velocityY,//
            "consumed",//
            consumed,//
            ""//
        ));
        return super.onNestedFling(parent, child, target, velocityX, velocityY, consumed);
    }
}
